﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;
using DevExpress.XtraTreeList.Nodes;

namespace ListsForm
{
    public partial class FrmEditRole : FrmBaseEditForm
    {
        public FrmEditRole(Lists.AccessAuthority role)
        {
            InitializeComponent();
            if (role == null)
            {
                _EditState = EditState.CreatingNew;
                bindingSource.AddNew();
                this.Text = "New AccessAuthority";
            }
            else
            {
                _EditState = EditState.Modifying;
                bindingSource.Add(DB.AccessAuthorities.FirstOrDefault(p => p.AccessAuthorityID == role.AccessAuthorityID));
                this.Text = "Edit AccessAuthority";
            }
            treeFunction.DataSource = DB.Functions.ToList();
            treeFunction.ExpandAll();
            if (_EditState == EditState.CreatingNew)
                CheckNode(null, treeFunction.Nodes);
            else 
                CheckNode(role.AccessAuthorityID, treeFunction.Nodes);
        }

        protected override void CheckFullInfo()
        {
            Lists.AccessAuthority role = (Lists.AccessAuthority)bindingSource.Current;
            if (string.IsNullOrEmpty(role.AccessAuthorityName))
                throw new Exception("AccessAuthority name not allow null");
        }

        protected override void SaveAndClose()
        {
            Lists.AccessAuthority role = (Lists.AccessAuthority)bindingSource.Current;
            if (_EditState == EditState.CreatingNew)
            {
                DB.AccessAuthorities.InsertOnSubmit(role);
                DB.SubmitChanges();
            }
            SaveToAccessAuthority_Function(role.AccessAuthorityID, treeFunction.Nodes);
            DB.SubmitChanges();
            this.Close();
        }

        private void CheckNode(int? roleID, TreeListNodes nodes)
        {
            foreach (TreeListNode node in nodes)
            {
                if (!roleID.HasValue)
                    node.Checked = false;
                else if (!DB.Role_Functions.Any(p => p.RoleID == roleID && p.FunctionID == (int)node.GetValue("FunctionID")))
                    node.Checked = false;
                else
                    node.Checked = true;
                CheckNode(roleID, node.Nodes);
            }
        }

        private void SaveToAccessAuthority_Function(int roleID, TreeListNodes nodes)
        {
            foreach (TreeListNode node in nodes)
            {
                if (!node.Checked)
                {
                    Lists.Role_Function rf = DB.Role_Functions.FirstOrDefault(p => p.RoleID == roleID && p.FunctionID == (int)node.GetValue("FunctionID"));
                    if (rf != null)
                        DB.Role_Functions.DeleteOnSubmit(rf);
                }
                else
                {
                    if (!DB.Role_Functions.Any(p => p.RoleID == roleID && p.FunctionID == (int)node.GetValue("FunctionID")))
                    {
                        Lists.Role_Function rf = new Lists.Role_Function();
                        rf.FunctionID = (int)node.GetValue("FunctionID");
                        rf.RoleID = roleID;
                        DB.Role_Functions.InsertOnSubmit(rf);
                    }
                }
                SaveToAccessAuthority_Function(roleID, node.Nodes);
            }
            DB.SubmitChanges();
        }
    }
}
